package com.ruoyi.project.weixin.mp.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.weixin.mp.entity.MsgTemplate;
import com.ruoyi.project.weixin.mp.entity.vo.MsgTemplateVo;
import com.ruoyi.project.weixin.mp.form.TemplateMsgBatchForm;
import com.ruoyi.project.weixin.mp.service.MsgTemplateService;
import com.ruoyi.project.weixin.mp.service.TemplateMsgService;
import com.ruoyi.project.weixin.mp.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.mp.api.WxMpService;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import me.chanjar.weixin.common.error.WxErrorException;

/**
 * 消息模板
 *
 * @author niefy
 * @email niefy@qq.com
 * @date 2019-11-12 18:30:15
 */
@RestController
@RequestMapping("/manage/msgTemplate")
@Api(tags = {"消息模板-管理后台","模板消息的模板"})
public class MsgTemplateManageController {

    @Autowired
    private MsgTemplateService msgTemplateService;

    @Autowired
    private TemplateMsgService templateMsgService;

    @Autowired
    private WxMpService wxMpService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('wx:msgtemplate:list')")
    @ApiOperation(value = "列表")
    public AjaxResult list(@RequestParam Map<String, Object> params) {
        PageUtils page = msgTemplateService.queryPage(params);
        List<MsgTemplateVo> list=new ArrayList<>();
        for (int i = 0; i < page.getList().size(); i++) {
            MsgTemplate msgTemplate = (MsgTemplate) page.getList().get(i);
            list.add(new MsgTemplateVo(msgTemplate));
        }
        page.setList(list);
        return AjaxResult.success(page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@ss.hasPermi('wx:msgtemplate:info')")
    @ApiOperation(value = "详情-通过ID")
    public AjaxResult info(@PathVariable("id") Long id) {
        MsgTemplate msgTemplate = msgTemplateService.getById(id);
        return AjaxResult.success(new MsgTemplateVo(msgTemplate));
    }

    /**
     * 信息
     */
    @GetMapping("/getByName")
    @PreAuthorize("@ss.hasPermi('wx:msgtemplate:info')")
    @ApiOperation(value = "详情-通过名称")
    public AjaxResult getByName(String name){
        MsgTemplate msgTemplate = msgTemplateService.selectByName(name);
        return AjaxResult.success(msgTemplate);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPermi('wx:msgtemplate:save')")
    @ApiOperation(value = "保存")
    public AjaxResult save(@RequestBody MsgTemplate msgTemplate) {
        msgTemplateService.save(msgTemplate);
        return AjaxResult.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @PreAuthorize("@ss.hasPermi('wx:msgtemplate:update')")
    @ApiOperation(value = "修改")
    public AjaxResult update(@RequestBody MsgTemplateVo msgTemplateVo) {
        msgTemplateService.updateById(new MsgTemplate(msgTemplateVo));
        return AjaxResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{ids}")
    @PreAuthorize("@ss.hasPermi('wx:msgtemplate:delete')")
    @ApiOperation(value = "删除")
    public AjaxResult delete(@PathVariable String[] ids) {
        msgTemplateService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }

    /**
     * 同步公众号模板
     */
    @PostMapping("/syncWxTemplate")
    @PreAuthorize("@ss.hasPermi('wx:msgtemplate:save')")
    @ApiOperation(value = "同步公众号模板")
    public AjaxResult syncWxTemplate(String appid) throws WxErrorException {
        this.wxMpService.switchoverTo(appid);
        msgTemplateService.syncWxTemplate(appid);
        return AjaxResult.success();
    }

    /**
     * 批量向用户发送模板消息
     * 通过用户筛选条件（一般使用标签筛选），将消息发送给数据库中所有符合筛选条件的用户
     */
    @PostMapping("/sendMsgBatch")
    @PreAuthorize("@ss.hasPermi('wx:msgtemplate:save')")
    @ApiOperation(value = "批量向用户发送模板消息",notes = "将消息发送给数据库中所有符合筛选条件的用户")
    public AjaxResult sendMsgBatch(@CookieValue String appid,@RequestBody TemplateMsgBatchForm form) {
        this.wxMpService.switchoverTo(appid);
        templateMsgService.sendMsgBatch(form, appid);
        return AjaxResult.success();
    }
}
